home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-05-23 | 21.6 KB | 834 lines |
- // ImageEditorComponent.java
- // A painting program able to load and save images via JFS
- import java.awt.*;
- import java.awt.image.ImageProducer;
- import java.awt.image.CropImageFilter;
- import java.awt.image.FilteredImageSource;
- import java.awt.image.PixelGrabber;
- import java.awt.image.MemoryImageSource;
- import java.util.Vector;
-
- public class ImageEditorComponent extends JFScomponent
- {
- Image image; // last loaded image
- ImageButton loadb, saveb, clearb, // file control buttons
- undob, printb, saveselb;
- ImageToggle freehandb, lineb, boxb, // drawing mode buttons
- circleb, brushb, handb,
- polygonb, floodfillb, textb,
- selectb;
- ImageButton fontb, prefsb; // prefs buttons
- ImageToggle emptyb, filledb; // solid/empty buttons
- ImageToggle sizeb[]=new ImageToggle[6]; // brush size buttons
- ColourCanvas col; // current colour display
- ColourSlider red,green,blue; // sliders for colour
- PaintingCanvas pc; // painting area
- BorderPanel mid; // panel canvas is in
- FileReq loadreq, savereq; // load and save requestors
-
- String mtype[] = {"image/ppm", "image/gif", "image/jpeg"};
-
- void init(java.applet.Applet a)
- {
- setLayout(new BorderLayout());
-
- // Create left button panel
- Panel left = new Panel();
- left.setLayout(new GridLayout(1,2));
-
- // Create left left column of buttons
- ControlPanel lleft = new ControlPanel(a);
- loadb = lleft.addbutton("images/load.gif");
- saveb = lleft.addbutton("images/save.gif");
- saveselb = lleft.addbutton("images/savesel.gif");
- lleft.addgap(7);
- ImageToggleGroup modeg = new ImageToggleGroup();
- handb = lleft.addtoggle("images/hand.gif", modeg, false);
- freehandb = lleft.addtoggle("images/freehand.gif", modeg, true);
- lineb = lleft.addtoggle("images/line.gif", modeg, false);
- boxb = lleft.addtoggle("images/box.gif", modeg, false);
- circleb = lleft.addtoggle("images/circle.gif", modeg, false);
- lleft.addgap(10);
- fontb = lleft.addbutton("images/font.gif");
- left.add(lleft);
-
- // Create right left column of buttons
- ControlPanel rleft = new ControlPanel(a);
- printb = rleft.addbutton("images/print.gif");
- clearb = rleft.addbutton("images/clear.gif");
- undob = rleft.addbutton("images/undo.gif");
- rleft.addgap(7);
- brushb = rleft.addtoggle("images/brush.gif", modeg, false);
- polygonb = rleft.addtoggle("images/polygon.gif", modeg, false);
- floodfillb = rleft.addtoggle("images/floodfill.gif", modeg, false);
- textb = rleft.addtoggle("images/text.gif", modeg, false);
- selectb = rleft.addtoggle("images/select.gif", modeg, false);
- left.add(rleft);
- rleft.addgap(10);
- prefsb = rleft.addbutton("images/prefs.gif");
- add("West",left);
-
- // Create right button panel
- ControlPanel right = new ControlPanel(a);
- ImageToggleGroup fillg = new ImageToggleGroup();
- emptyb = right.addtoggle("images/empty.gif", fillg, true);
- filledb = right.addtoggle("images/filled.gif", fillg, false);
- right.addgap(7);
- ImageToggleGroup sizeg = new ImageToggleGroup();
- sizeb[0] = right.addtoggle("images/size1.gif", sizeg, true);
- sizeb[1] = right.addtoggle("images/size2.gif", sizeg, false);
- sizeb[2] = right.addtoggle("images/size3.gif", sizeg, false);
- sizeb[3] = right.addtoggle("images/size4.gif", sizeg, false);
- sizeb[4] = right.addtoggle("images/size5.gif", sizeg, false);
- sizeb[5] = right.addtoggle("images/size6.gif", sizeg, false);
- add("East",right);
-
- // Create bottom colour choosers
- Panel bot = new Panel();
- bot.setLayout(new BorderLayout());
- bot.add("West",col = new ColourCanvas());
- SpacedPanel sliders = new SpacedPanel(2,0);
- sliders.setLayout(new GridLayout(3,1));
- sliders.add(red = new ColourSlider('r'));
- sliders.add(green = new ColourSlider('g'));
- sliders.add(blue = new ColourSlider('b'));
- bot.add("Center", sliders);
- add("South",bot);
-
- // create main painting area
- mid = new BorderPanel(Color.black, new Color(230,230,230));
- mid.setLayout(new GridLayout(1,1));
- mid.add("Center",pc = new PaintingCanvas(this));
- add("Center",mid);
- }
-
- public void connect(JFSclient c)
- {
- super.connect(c);
- try if (!client.canaccess("/dev/Printer", 'w')) printb.disable();
- catch(RequestException e);
- }
-
- // load
- // Load the given file, and wait while it is converted from PPM format
- void load(String file, int ver) throws RequestException
- {
- byte fd[] = client.get(file, ver);
- BufferInputStream buf = new BufferInputStream(fd);
- Image i = createImage(new PPMloader(buf));
- MediaTracker waiter = new MediaTracker(this);
- waiter.addImage(i, 666);
- try waiter.waitForAll();
- catch(InterruptedException e);
- if (waiter.isErrorAny()) new ErrorWindow("Error loading image");
- else {
- if (pc != null) pc.setimage(i);
- else image = i;
- }
- client.setcurrent(file);
- }
-
- Dimension wantedsize()
- {
- return new Dimension(600,400);
- }
-
- void print(String printer, String type) throws RequestException
- {
- if (!type.equals("PPM"))
- throw new RequestException("Only PPM printers are supported");
- BufferOutputStream buf = new BufferOutputStream();
- ImageProducer src;
- if (pc != null) src = pc.getimage().getSource();
- else src = image.getSource();
- PPMsaver sv = new PPMsaver();
- src.startProduction(sv);
- sv.output(buf, true);
- src.removeConsumer(sv);
- Message pmsg = new Message();
- pmsg.add("File","/dev/Printer");
- pmsg.add("Printer",printer);
- pmsg.setdata(buf.getarray());
- try client.send("Put", pmsg);
- catch(RequestException e)
- throw new RequestException("Error writing to printer");
- }
-
- // action
- // Handle a user command
- public boolean action(Event e, Object o)
- {
- // File button set
- if (e.target == loadb && loadreq == null) {
- loadreq = new FileReq(client, this, "Load", "image/*",
- false, null);
- }
- else if (e.target == saveb && pc.getimage() != null) {
- savereq = new FileReq(client, this, "Save", "image/*",
- true, mtype);
- }
- else if (e.target == clearb)
- new ImageSizeReq(pc);
- else if (e.target == undob)
- pc.undo();
- else if (e.target == printb)
- new PrintRequestor(this, client);
-
- // Drawing mode button set
- else if (e.target == freehandb)
- pc.setmode(PaintingCanvas.FREEHAND);
- else if (e.target == lineb)
- pc.setmode(PaintingCanvas.LINE);
- else if (e.target == boxb)
- pc.setmode(PaintingCanvas.BOX);
- else if (e.target == circleb)
- pc.setmode(PaintingCanvas.CIRCLE);
- else if (e.target == brushb)
- pc.setmode(PaintingCanvas.BRUSH);
- else if (e.target == handb)
- pc.setmode(PaintingCanvas.HAND);
- else if (e.target == polygonb)
- pc.setmode(PaintingCanvas.POLYGON);
- else if (e.target == textb)
- pc.setmode(PaintingCanvas.TEXT);
- else if (e.target == selectb)
- pc.setmode(PaintingCanvas.SELECT);
- else if (e.target == floodfillb)
- pc.setmode(PaintingCanvas.FLOODFILL);
-
- // Prefs button set
- else if (e.target == fontb) {
- new FontRequestor(this, pc.getfont());
- }
- else if (e.target == prefsb) {
- }
-
- // Colour controls
- if (e.target == red || e.target == green || e.target == blue) {
- // change the current drawing colour
- Color nc = new Color(red.getvalue(),
- green.getvalue(),
- blue.getvalue());
- col.set(nc);
- if (pc != null) pc.setcolour(nc);
- }
-
- // File requestors
- else if (e.target == loadreq) {
- if (((String)e.arg).equals("Load")) {
- // load a file
- try load(loadreq.getfile(), loadreq.getversion());
- catch(RequestException ex)
- new ErrorWindow("Could not open "+
- loadreq.getfile()+" : "+
- ex.getMessage());
- }
- loadreq = null;
- }
- else if (e.target == savereq) {
- if (((String)e.arg).equals("Save")) {
- // Write image data to an array in the correct format
- String type = savereq.gettype();
- BufferOutputStream buf = new BufferOutputStream();
- ImageProducer src = pc.getimage().getSource();
- if (type.equals("image/ppm")) {
- PPMsaver sv = new PPMsaver();
- src.startProduction(sv);
- sv.output(buf, true);
- src.removeConsumer(sv);
- }
- else {
- new ErrorWindow("Cannot save format "+type);
- return false;
- }
-
- // Write out the data
- try save(buf.getarray(), savereq.getfile(), type,
- savereq.getversion(), savereq.multiversion());
- catch(RequestException ex)
- new ErrorWindow("Could not save "+
- savereq.getfile()+" : "+
- ex.getMessage());
- }
- savereq = null;
- }
-
- // Font chosen
- else if (e.target instanceof FontRequestor)
- pc.setfont(((FontRequestor)e.target).getfont());
-
- // Fill type button set
- else if (e.target == filledb)
- pc.setfillmode(PaintingCanvas.FILLED);
- else if (e.target == emptyb)
- pc.setfillmode(PaintingCanvas.EMPTY);
-
- // Brush size button set
- else {
- for(int i=0; i<sizeb.length; i++)
- if (e.target == sizeb[i])
- pc.setbrushsize(i+1);
- }
-
- return true;
- }
- }
-
- // ColourCanvas
- // A box displaying a single solid colour.
- class ColourCanvas extends Canvas
- {
- int red,green,blue;
- int border = 3;
- Color col = Color.black;
- Color hi = new Color(230,230,230), lo = new Color(50,50,50);
-
- // set
- // Set the colour being displayed
- void set(Color c)
- {
- col = c;
- paint(getGraphics());
- }
-
- public void paint(Graphics g)
- {
- int w = size().width, h = size().height;
- g.setColor(col);
- g.fillRect(0, 0, w, h);
- g.setColor(hi);
- for(int i=0; i<border; i++) {
- g.drawLine(i,i,w-i,i);
- g.drawLine(i,i,i,h-i);
- }
- g.setColor(lo);
- for(int i=0; i<border; i++) {
- g.drawLine(w-i,h-i, w-i,i);
- g.drawLine(w-i,h-i, i,h-i);
- }
- }
-
- public Dimension minimumSize()
- {
- return new Dimension(51,51);
- }
-
- public Dimension preferredSize()
- {
- return minimumSize();
- }
- }
-
- // ColourSlider
- // A slider for choosing a red, green or blue value
- class ColourSlider extends Canvas
- {
- char primary;
- Image im; // image containing a smooth colour range
- int w,h; // size of this component
- int pos = 0; // slider position (0-255)
-
- // Construct a new slider for the given primary
- ColourSlider(char p) { primary = p; }
-
- // Return the current colour value, as a number from 0-255
- int getvalue() { return pos; }
-
- public void reshape(int nx, int ny, int nw, int nh)
- {
- if (nw <= 2 || nh <= 2)
- invalidate();
- else if (nw != w || nh != h) {
- w = nw; h = nh;
- im = createImage(w-2,h-2);
- Graphics g = im.getGraphics();
- int val = -1;
- for(int i=0; i<w-2; i++) {
- if (i*256/w != val) {
- val = i*256/w;
- g.setColor(new Color(primary=='r'?val:0,
- primary=='g'?val:0,
- primary=='b'?val:0));
- }
- g.drawLine(i, 1, i, h-1);
- }
- }
- super.reshape(nx, ny, nw, nh);
- }
-
- public boolean mouseDown(Event e, int x, int y)
- {
- return mouseDrag(e, x, y);
- }
-
- public boolean mouseDrag(Event e, int x, int y)
- {
- pos = Math.min(Math.max(x*256/w,0),255);
- paint(getGraphics());
- getParent().postEvent(new Event(this, Event.ACTION_EVENT, null));
- return true;
- }
-
- // paint
- // Render this slider
- public void paint(Graphics g)
- {
- int x = pos*w/256;
- g.drawImage(im, 1, 1, this);
- g.setColor(Color.white);
- g.fillRect(x-2, 1, 5, h-2);
- }
-
- public Dimension minimumSize()
- {
- return new Dimension(50,10);
- }
-
- public Dimension preferredSize()
- {
- return minimumSize();
- }
- }
-
- // PaintingCanvas
- // A component for doing drawing on
- class PaintingCanvas extends Canvas
- {
- ImageEditorComponent parent;
- Image bogusim; // an unusable image waiting to be converted
- Image im; // image being edited, or null
- Graphics img; // graphics of im
- Image uim; // undo image
- int xoff,yoff; // offset of image from corner
- int xhand, yhand; // where the hand tool started dragging
- int mode = FREEHAND;
- int brushsz = 1;
- int fillmode = EMPTY;
- Color col = Color.black;
- Polygon poly = null; // polygon being drawn
- Font fn = new Font("TimesRoman", Font.PLAIN, 10);
- static final int FREEHAND = 0; // draw modes
- static final int LINE = 1;
- static final int BOX = 2;
- static final int CIRCLE = 3;
- static final int BRUSH = 4;
- static final int HAND = 5;
- static final int POLYGON = 6;
- static final int TEXT = 7;
- static final int SELECT = 8;
- static final int FLOODFILL = 9;
- static final int FILLED = 0; // fill modes for solid shapes
- static final int EMPTY = 1;
-
- PaintingCanvas(ImageEditorComponent p)
- {
- parent = p;
- }
-
- // setimage
- // Set the image currently being edited
- void setimage(Image i)
- {
- bogusim = i;
- if (isShowing()) convert();
- }
-
- // setmode
- // Set the current drawing mode. Must be one of the ones listed above
- void setmode(int m) { mode = m; poly = null; }
-
- // setcolour
- // Set the current drawing colour
- void setcolour(Color c) { col = c; }
-
- // setbrushsize
- // Set the current brush size
- void setbrushsize(int b) { brushsz = b; }
-
- // setfillmode
- // Set the mode used for filling boxes, ovals and polygons
- void setfillmode(int m) { fillmode = m; }
-
- // setfont
- // Set the font for text drawing
- void setfont(Font f) { fn = f; }
-
- // getfont
- // Gets the currently set font
- Font getfont() { return fn; }
-
- // undo
- // Copy the undo image to the current image, and blit it to the front
- void undo()
- {
- if (img == null) return;
- img.drawImage(uim, 0, 0, this);
- blit();
- }
-
- // getimage
- // Returns the image currently being edited
- Image getimage()
- {
- if (im == null && bogusim != null)
- return bogusim;
- return im;
- }
-
- // convert
- // Copy the image in bogusim to another image so it can be actually
- // used. This can only be called when the component is showing.
- void convert()
- {
- im = createImage(bogusim.getWidth(this), bogusim.getHeight(this));
- uim = createImage(bogusim.getWidth(this), bogusim.getHeight(this));
- img = im.getGraphics();
- img.drawImage(bogusim, 0, 0, this);
- uim.getGraphics().drawImage(bogusim, 0, 0, this);
- bogusim = null;
- paint(getGraphics());
- }
-
- // blit
- // Copy the current image to the front (assumes there IS a current
- // image)
- void blit()
- {
- getGraphics().drawImage(im, xoff, yoff, this);
- }
-
- // backup
- // Copy the current image to the undo buffer
- void backup()
- {
- uim.getGraphics().drawImage(im, 0, 0, this);
- }
-
- // paint
- // Clear the front, draw the image and surround it with a nice border
- public void paint(Graphics g)
- {
- if (bogusim != null) convert();
- g.setColor(Color.lightGray);
- g.fillRect(0, 0, size().width, size().height);
- if (im == null) return; // no image
- blit();
- g.setColor(Color.black);
- g.drawRect(xoff-1, yoff-1,
- im.getWidth(this)+1, im.getHeight(this)+1);
- }
-
- int lx,ly; // last click point
-
- public boolean mouseDown(Event e, int sx, int sy)
- {
- if (im == null) return false;
- lx = Math.min(Math.max(sx-xoff,0),im.getWidth(this));
- ly = Math.min(Math.max(sy-yoff,0),im.getHeight(this));
- if (mode == POLYGON) {
- if (poly == null) {
- backup(); // 1st point
- poly = new Polygon();
- poly.addPoint(lx, ly);
- }
- if (poly.npoints >= 3 &&
- Math.abs(lx - poly.xpoints[poly.npoints-2]) < 3 &&
- Math.abs(ly - poly.ypoints[poly.npoints-2]) < 3) {
- // last point clicked
- poly = null;
- }
- else poly.addPoint(lx, ly);
- }
- else if (mode == HAND) {
- xhand = lx;
- yhand = ly;
- }
- else
- backup();
- requestFocus(); // so we get chars in TEXT mode
- return true;
- }
-
- public boolean mouseDrag(Event e, int sx, int sy)
- {
- if (im == null) return false;
- int rx = Math.min(Math.max(sx-xoff,0),im.getWidth(this));
- int ry = Math.min(Math.max(sy-yoff,0),im.getHeight(this));
- img.setColor(col);
- if (mode == FREEHAND) {
- img.drawLine(lx, ly, rx, ry);
- blit();
- lx = rx; ly = ry;
- }
- else if (mode == LINE) {
- undo();
- img.drawLine(lx, ly, rx, ry);
- blit();
- }
- else if (mode == BOX) {
- undo();
- int x = Math.min(lx,rx), y = Math.min(ly,ry);
- int w = Math.abs(lx-rx), h = Math.abs(ly-ry);
- if (fillmode == EMPTY) img.drawRect(x, y, w, h);
- else img.fillRect(x, y, w, h);
- blit();
- }
- else if (mode == CIRCLE) {
- undo();
- int w = Math.abs(lx-rx), h = Math.abs(ly-ry);
- if (fillmode == EMPTY) img.drawOval(lx-w, ly-h, w*2, h*2);
- else img.fillOval(lx-w, ly-h, w*2, h*2);
- blit();
- }
- else if (mode == BRUSH) {
- img.fillOval(rx-brushsz, ry-brushsz, brushsz*2, brushsz*2);
- blit();
- }
- else if (mode == HAND) {
- if (im.getWidth(this) > size().width) {
- xoff = sx - xhand;
- if (xoff > 0)
- xoff = 0;
- else if (xoff < size().width-im.getWidth(this))
- xoff = size().width-im.getWidth(this);
- }
- if (im.getHeight(this) > size().height) {
- yoff = sy - yhand;
- if (yoff > 0)
- yoff = 0;
- else if (yoff < size().height-im.getHeight(this))
- yoff = size().height-im.getHeight(this);
- }
- blit();
- }
- else if (mode == SELECT) {
- undo();
- int x = Math.min(lx,rx), y = Math.min(ly,ry);
- int w = Math.abs(lx-rx), h = Math.abs(ly-ry);
- img.drawRect(x, y, w, h);
- blit();
- }
- return true;
- }
-
- public boolean mouseMove(Event e, int sx, int sy)
- {
- if (mode == POLYGON && poly != null) {
- int rx = Math.min(Math.max(sx-xoff,0),im.getWidth(this));
- int ry = Math.min(Math.max(sy-yoff,0),im.getHeight(this));
- undo();
- img.setColor(col);
- poly.xpoints[poly.npoints-1] = rx;
- poly.ypoints[poly.npoints-1] = ry;
- if (fillmode == EMPTY || poly.npoints < 3)
- img.drawPolygon(poly);
- else
- img.fillPolygon(poly);
- blit();
- }
- return true;
- }
-
- public boolean mouseUp(Event e, int sx, int sy)
- {
- if (im == null) return false;
- int rx = Math.min(Math.max(sx-xoff,0),im.getWidth(this));
- int ry = Math.min(Math.max(sy-yoff,0),im.getHeight(this));
- if (mode == SELECT) {
- // when in select mode, dragging out an area copies to
- // the clipboard, and clicking at some point pastes.
- if (Math.abs(lx - rx) < 3 && Math.abs(ly - ry) < 3) {
- // Paste if we can
- Object cbuf = parent.client.getbuf();
- if (cbuf != null && cbuf instanceof Image) {
- img.drawImage((Image)cbuf, rx, ry, this);
- blit();
- }
- }
- else {
- // Copy selected area
- undo();
- int x = Math.min(lx,rx), y = Math.min(ly,ry);
- int w = Math.abs(lx-rx), h = Math.abs(ly-ry);
- Image cut = createImage(
- new FilteredImageSource(im.getSource(),
- new CropImageFilter(x, y, w, h)));
- parent.client.setbuf(cut);
- }
- }
- else if (mode == FLOODFILL) {
- // Get image pixels
- int w = im.getWidth(this), h = im.getHeight(this);
- int arr[] = new int[w * h];
- PixelGrabber grab = new PixelGrabber(im, 0, 0, w, h, arr, 0, w);
- try grab.grabPixels(1);
- catch(InterruptedException ex) {
- return true;
- }
-
- // Flood from seed point, staying within seed colour
- int c = (255<<24) + (col.getRed()<<16) +
- (col.getGreen()<<8) + col.getBlue();
- int p = arr[ry*w + rx];
- if (p != c) flood(arr, w, h, rx, ry, p, c);
-
- // Convert pixels back to an image
- Image back = createImage(
- new MemoryImageSource(w, h, arr, 0, w));
- img.drawImage(back, 0, 0, this);
- blit();
- }
- return true;
- }
-
- public boolean keyDown(Event e, int k)
- {
- if (mode == TEXT) {
- char c = (char)k;
- img.setColor(col);
- if (c >= 32 && c < 127) {
- // Draw a character at the current position
- String s = String.valueOf(c);
- img.setFont(fn);
- img.drawString(s, lx, ly);
- lx += img.getFontMetrics().stringWidth(s);
- }
- blit();
- }
- return true;
- }
-
- // flood
- // Floodfill an array of pixels, using an iterative stack-based
- // algorithm. BUG - the allocated stack is way too big.. what is
- // a proper upper bound for flooding an w*h image?
- void flood(int arr[], int w, int h, int x, int y, int b, int c)
- {
- int xstk[] = new int[w*h], ystk[] = new int[w*h];
- int sp = 0;
- xstk[sp] = x; ystk[sp] = y;
- sp++;
- while(sp != 0) {
- // Remove the top pixel from the stack, and set it
- sp--;
- int px = xstk[sp], py = ystk[sp];
- int pos = py*w + px;
- arr[pos] = c;
-
- // Check surrounding pixels, and if OK add them to the stack
- if (px > 0 && arr[pos-1] == b)
- { xstk[sp] = px-1; ystk[sp] = py; sp++; }
- if (py > 0 && arr[pos-w] == b)
- { xstk[sp] = px; ystk[sp] = py-1; sp++; }
- if (px < w-1 && arr[pos+1] == b)
- { xstk[sp] = px+1; ystk[sp] = py; sp++; }
- if (py < h-1 && arr[pos+w] == b)
- { xstk[sp] = px; ystk[sp] = py+1; sp++; }
- }
- }
- }
-
- // ControlPanel
- // A panel containing a vertical array of ImageButtons, each set at their
- // preferred sizes and positioned directly below each other.
- class ControlPanel extends Panel
- {
- java.applet.Applet ap; // applet for loading images
- int w=0, h=0;
-
- ControlPanel(java.applet.Applet a)
- {
- ap = a;
- setLayout(null);
- }
-
- // addbutton
- // Create and add an ImageButton from the given file
- ImageButton addbutton(String file)
- {
- Image i = ap.getImage(ap.getCodeBase(),file);
- ImageButton b = new ImageButton(i);
- Dimension bs = b.preferredSize();
- add(b);
- b.reshape(0, h, bs.width, bs.height);
- w = Math.max(w, bs.width);
- h += bs.height;
- return b;
- }
-
- // addtoggle
- // Create and add an ImageToggle from the given file
- ImageToggle addtoggle(String file, ImageToggleGroup g, boolean s)
- {
- Image i = ap.getImage(ap.getCodeBase(),file);
- ImageToggle t = new ImageToggle(i, s, g);
- Dimension ts = t.preferredSize();
- t.reshape(0, h, ts.width, ts.height);
- add(t);
- w = Math.max(w, ts.width);
- h += ts.height;
- return t;
- }
-
- // addgap
- // Add a p-pixel gap between buttons
- void addgap(int p)
- {
- h += p;
- }
-
- public Dimension preferredSize() { return new Dimension(w,h); }
- public Dimension minimumSize() { return preferredSize(); }
- }
-
-
- // ImageSizeReq
- // A requestor that asks for the size of a new image.
- class ImageSizeReq extends FixedFrame
- {
- PaintingCanvas canvas;
- TextField width, height;
- Button ok, can;
-
- ImageSizeReq(PaintingCanvas c)
- {
- super(new Dimension(200, 150));
- canvas = c;
- setLayout(new BorderLayout());
- Panel top = new Panel();
- top.setLayout(new GridLayout(2,2));
- top.add(new Label("Width"));
- top.add(width = new TextField("300"));
- top.add(new Label("Height"));
- top.add(height = new TextField("300"));
- add("North",top);
- Panel bot = new Panel();
- bot.setLayout(new FlowLayout(FlowLayout.RIGHT));
- bot.add(ok = new Button("Ok"));
- bot.add(can = new Button("Cancel"));
- add("South",bot);
- setTitle("Image size");
- pack();
- show();
- }
-
- public boolean handleEvent(Event e)
- {
- if (e.id == Event.WINDOW_DESTROY ||
- e.id == Event.ACTION_EVENT && e.target == can)
- dispose();
- else if (e.id == Event.ACTION_EVENT && e.target == ok) {
- // new width and height entered
- int w = Integer.parseInt(width.getText());
- int h = Integer.parseInt(height.getText());
- if (w > 0 && h > 0)
- canvas.setimage(createImage(w,h));
- else
- new ErrorWindow("Bogus dimensions");
- dispose();
- }
- return super.handleEvent(e);
- }
- }
-
-